/*
 * ============================================================================
 *
 *       Filename:  apstatus.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2014年09月01日 15时27分34秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "aplua.h"
#include "common.h"
#include "apstatus.h"

char *apnetwork(char *nic)
{
	assert(luahandle.L_apstatus != NULL);
	lua_State *L = luahandle.L_apstatus;

	/* invoke lua func */
	acctl_getglobal(L, "apnetwork");
	/* params */
	lua_pushstring(L, nic);
	acctl_pcall(L, 1, 1);

	if(!lua_isstring(L, -1)) {
		sys_err("Lua function apnetwork should"
			"return a string\n");
		exit(-1);
	}

	char *s = lua_tostring(L, -1);
	char *d = safe_malloc(strlen(s) + 1);
	strcpy(d, s);
	lua_pop(L, 1);
	return d;
}

char *apstatus()
{
	assert(luahandle.L_apstatus != NULL);
	lua_State *L = luahandle.L_apstatus;

	/* invoke lua func */
	acctl_getglobal(L, "apstatus");
	acctl_pcall(L, 0, 1);

	if(!lua_isstring(L, -1)) {
		sys_err("Lua function apstatus should"
			"return a string\n");
		exit(-1);
	}

	char *s = lua_tostring(L, -1);
	char *d = safe_malloc(strlen(s) + 1);
	strcpy(d, s);
	lua_pop(L, 1);
	return d;
}

